//
//  NSCommonTextView.m
//  NSManagement
//
//  Created by jinglian on 16/11/8.
//  Copyright © 2016年 NSManagement. All rights reserved.
//

#import "NSCommonTextView.h"
//#import "KLNotificationHelp.h"

#define KLColor(r, g, b)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

@interface NSCommonTextView ()

/** 占位文字View: 与当前textView,文字重叠显示*/
@property (nonatomic, weak) UITextView *placeholderView ;
@end

@implementation NSCommonTextView

- (void)dealloc {
//    [KLNotificationHelp removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setup];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.scrollEnabled = YES;
    self.scrollsToTop = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.enablesReturnKeyAutomatically = YES;
    self.layer.cornerRadius = 5;
    
    //    self.textContainer.lineFragmentPadding = 0;
    //    self.textContainerInset = UIEdgeInsetsZero;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    
    self.ns_placeholderColor = KLColor(187, 186, 196);
}

- (void)textDidChange {
    self.placeholderView.hidden = self.text.length > 0;
}

- (void)setNs_placeholderColor:(UIColor *)ns_placeholderColor {
    _ns_placeholderColor = ns_placeholderColor;
    
    self.placeholderView.textColor = ns_placeholderColor;
}

- (void)setNs_placeholder:(NSString *)ns_placeholder {
    _ns_placeholder = ns_placeholder;
    
    self.placeholderView.text = ns_placeholder;
}

- (void)setText:(NSString *)text {
    [super setText:text];
    
    self.placeholderView.hidden = text.length != 0;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.placeholderView.frame = self.bounds;
}

#pragma mark - Lazy
- (UITextView *)placeholderView {
    if (_placeholderView == nil) {
        UITextView *placeholderView = [[UITextView alloc] init];
        _placeholderView = placeholderView;
        _placeholderView.scrollEnabled = NO;
        _placeholderView.showsHorizontalScrollIndicator = NO;
        _placeholderView.showsVerticalScrollIndicator = NO;
        _placeholderView.userInteractionEnabled = NO;
        _placeholderView.font = self.font;
        _placeholderView.backgroundColor = [UIColor clearColor];
        _placeholderView.frame = self.bounds;
        //        _placeholderView.textContainer.lineFragmentPadding = 0;
        //        _placeholderView.textContainerInset = UIEdgeInsetsZero;
        //        _placeholderView.textColor = KLColor(240, 240, 240);
        [self addSubview:placeholderView];
    }
    return _placeholderView;
}


@end
